package com.mairuide.bill.methodsanalysis.bszjzy.controller;

import com.mairuide._frame.base.BaseController;
import com.mairuide._frame.base.ModuleCode;
import com.mairuide.bill.methodsanalysis.bszjzy.dto.BszizyDto;
import com.mairuide.bill.methodsanalysis.bszjzy.pojo.BszizyEntity_YH;
import com.mairuide.bill.methodsanalysis.bszjzy.service.BszizyService;
import com.mairuide.bill.utils.DownloadUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-06-03 16:16
 * 伴随资金转移
 */
@Controller
@RequestMapping("/bszjzy")
public class BszizyController extends BaseController {
    @Autowired
    private BszizyService bszizyService;

    /**
     * 常用战法分析共同日期查询入口
     * @param model
     * @param pagesupport
     * @return
     */
    @ModuleCode(pValue = "billskill", value = "bszjzy")
    @RequestMapping(value = "/bszjzyInit", method = RequestMethod.GET)
    public Object init() {
        return "/methodsanalysis/bszjzy";
    }
    @RequestMapping(value = "/bszjzyInitDialog", method = RequestMethod.GET)
    public Object Dialog() {
        return "/methodsanalysis/bszjzyDialog";
    }
    @RequestMapping(value = "/bszjzyDiffDayInitDialog", method = RequestMethod.GET)
    public Object DiffDayDialog() {
        return "/methodsanalysis/bszjzyDiffDayDialog";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object findBSZJZY(BszizyDto dto){
        List<BszizyEntity_YH> entityList = bszizyService.findBSZJZY_youhua2(dto);
        return entityList;
    }
    @RequestMapping("/detail")
    @ResponseBody
    public Object detail(BszizyDto dto){
        List bszjzy_detail = bszizyService.findBSZJZY_detail_youhua2(dto);
        return bszjzy_detail;
    }
    @RequestMapping("/diffday_detail")
    @ResponseBody
    public Object diffday_detail(BszizyDto dto){
        List bszjzy_detail = bszizyService.findBSZJZY_total_day_detail(dto);
        return bszjzy_detail;
    }

    @RequestMapping("/downExcel")
    public void downExcel(BszizyDto dto){
        try {
            //第一步 得到数据
            List<BszizyEntity_YH>  list = bszizyService.findBSZJZY_youhua2(dto);
            //第二步 下载excel
            //new ExcelExportUtil(BszizyEntity.class).export(response,entityList,"伴随资金分析.xlsx");
            //Workbook wb = new XSSFWorkbook();
            SXSSFWorkbook wb = new SXSSFWorkbook(100); //阈值，内存中的对象数量最大数量
            //构造sheet
            Sheet sheet = wb.createSheet();
            //标题
            String [] titles = "交易主体,交易主体卡号,交易对手,交易对手卡号,交易次数,伴随转移账户户名,伴随转移卡号,交易总次数,收付标志".split(",");
            Row row = sheet.createRow(0);
            int titleIndex=0;
            for (String title : titles) {
                Cell cell = row.createCell(titleIndex++);
                cell.setCellValue(title);
            }
            int rowIndex = 1;
            Cell cell=null;
            List<Integer> c_list = new ArrayList<>();
            List<String> c_name = new ArrayList<>();
            for (BszizyEntity_YH m : list) {
                row = sheet.createRow(rowIndex++);
                String account_BODY = m.getACCOUNT_BODY();
                String account_BODY_CARD = m.getACCOUNT_BODY_CARD();
                String account_RIVAL = m.getACCOUNT_RIVAL();
                String account_RIVAL_CARD = m.getACCOUNT_RIVAL_CARD();
                String account_RIVAL2 = m.getACCOUNT_RIVAL2();
                String account_RIVAL_CARD2 = m.getACCOUNT_RIVAL_CARD2();
                Integer count =  m.getCOUNT();
                String sign = m.getSIGN()== 1 ? "支出":"收入";
                Integer c = m.getC();
                Integer rival_count = m.getRIVAL_COUNT();
                cell = row.createCell(0);
                cell.setCellValue(account_BODY);
                cell = row.createCell(1);
                cell.setCellValue(account_BODY_CARD);
                cell = row.createCell(2);
                cell.setCellValue(account_RIVAL);
                cell = row.createCell(3);
                cell.setCellValue(account_RIVAL_CARD);
                cell = row.createCell(4);
                cell.setCellValue(rival_count);
                cell = row.createCell(5);
                cell.setCellValue(account_RIVAL2);
                cell = row.createCell(6);
                cell.setCellValue(account_RIVAL_CARD2);
                cell = row.createCell(7);
                cell.setCellValue(count);
                cell = row.createCell(8);
                cell.setCellValue(sign);

                String name = account_BODY+"-"+account_BODY_CARD+"-"+account_RIVAL+"-"+account_RIVAL_CARD;
                if (!c_name.contains(name)){
                    c_name.add(name);
                    c_list.add(c);
                }
            }
            int rowspan = 1;
            for (int i = 0; i < c_list.size(); i++) {
                Integer c= c_list.get(i);
                if (c>1){
                    CellRangeAddress region1 = new CellRangeAddress(rowspan, rowspan+c-1, 0, 0);
                    CellRangeAddress region2 = new CellRangeAddress(rowspan, rowspan+c-1, 1, 1);
                    CellRangeAddress region3= new CellRangeAddress(rowspan, rowspan+c-1, 2, 2);
                    CellRangeAddress region4 = new CellRangeAddress(rowspan, rowspan+c-1, 3, 3);
                    CellRangeAddress region5 = new CellRangeAddress(rowspan, rowspan+c-1, 4, 4);
                    sheet.addMergedRegion(region1);
                    sheet.addMergedRegion(region2);
                    sheet.addMergedRegion(region3);
                    sheet.addMergedRegion(region4);
                    sheet.addMergedRegion(region5);
                    rowspan+=c;
                }else{
                    rowspan+=1;
                }
            }

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            wb.write(os);
            new DownloadUtils().download(os,response,"伴随资金转移.xlsx");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/downDetailExcel")
    public void downDetailExcel(BszizyDto dto){
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //第一步 得到数据
            List<Map<String, Object>> list = bszizyService.findBSZJZY_detail_youhua2(dto);
            SXSSFWorkbook wb = new SXSSFWorkbook(100); //阈值，内存中的对象数量最大数量
            //构造sheet
            Sheet sheet = wb.createSheet();
            //标题
            String [] titles = "交易主体,交易主体账号,交易主体卡号,交易主体归属行,交易对手,交易对手账号,交易对手卡号,交易对手归属行,交易金额,交易后余额,收付标志,交易日期,备注".split(",");
            Row row = sheet.createRow(0);
            int titleIndex=0;
            for (String title : titles) {
                Cell cell = row.createCell(titleIndex++);
                cell.setCellValue(title);
            }
            int rowIndex = 1;
            Cell cell=null;
            for (Map<String, Object> m : list) {
                row = sheet.createRow(rowIndex++);
                String ACCOUNT_BODY = (String) m.get("ACCOUNT_BODY");
                String ACCOUNT_BODY_NUMBER = (String)m.get("ACCOUNT_BODY_NUMBER");
                String ACCOUNT_BODY_CARD = (String)m.get("ACCOUNT_BODY_CARD");
                String ACCOUNT_BODY_BANK = (String)m.get("ACCOUNT_BODY_BANK");


                String ACCOUNT_RIVAL = (String) m.get("ACCOUNT_RIVAL");
                String ACCOUNT_RIVAL_NUMBER = (String)m.get("ACCOUNT_RIVAL_NUMBER");
                String ACCOUNT_RIVAL_CARD = (String)m.get("ACCOUNT_RIVAL_CARD");
                String ACCOUNT_RIVAL_BANK = (String)m.get("ACCOUNT_RIVAL_BANK");


                String ACCOUNT_MONEY = new BigDecimal(m.get("ACCOUNT_MONEY").toString()).toString();
                String SURPLUS_MONEY = new BigDecimal(m.get("SURPLUS_MONEY").toString()).toString();

                String SIGN = Integer.valueOf(m.get("SIGN").toString()) == 1 ? "支出":"收入";

                String TRADE_DATE = sdf.format((Timestamp)m.get("TRADE_DATE"));
                String REMARKS = (String)m.get("REMARKS");

                cell = row.createCell(0);
                cell.setCellValue(ACCOUNT_BODY);
                cell = row.createCell(1);
                cell.setCellValue(ACCOUNT_BODY_NUMBER);
                cell = row.createCell(2);
                cell.setCellValue(ACCOUNT_BODY_CARD);
                cell = row.createCell(3);
                cell.setCellValue(ACCOUNT_BODY_BANK);
                cell = row.createCell(4);
                cell.setCellValue(ACCOUNT_RIVAL);
                cell = row.createCell(5);
                cell.setCellValue(ACCOUNT_RIVAL_NUMBER);
                cell = row.createCell(6);
                cell.setCellValue(ACCOUNT_RIVAL_CARD);
                cell = row.createCell(7);
                cell.setCellValue(ACCOUNT_RIVAL_BANK);
                cell = row.createCell(8);
                cell.setCellValue(ACCOUNT_MONEY);
                cell = row.createCell(9);
                cell.setCellValue(SURPLUS_MONEY);
                cell = row.createCell(10);
                cell.setCellValue(SIGN);
                cell = row.createCell(11);
                cell.setCellValue(TRADE_DATE);
                cell = row.createCell(12);
                cell.setCellValue(REMARKS);
            }
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            wb.write(os);
            new DownloadUtils().download(os,response,"伴随资金转移详情.xlsx");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
